package com.example.blackbox.Activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.example.blackbox.Broadcast.MineBroadcast;
import com.example.blackbox.Data.MineInfo;
import com.example.blackbox.R;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

public class MineInfoActivity extends Activity {
    EditText name,autograph,brithday,sex;
    Button save;
    ImageView head;
    FrameLayout fl;
    SharedPreferences sp;
    Bitmap bitmap;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_info);
        init();
    }

    public void init(){
        sp=getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        name=(EditText)findViewById(R.id.mine_info_et_name);
        autograph=(EditText)findViewById(R.id.mine_info_et_autograph);
        brithday=(EditText)findViewById(R.id.mine_info_et_brithday);
        sex=(EditText)findViewById(R.id.mine_info_et_sex);
        save=(Button)findViewById(R.id.mine_info_bt_save);
        fl=(FrameLayout)findViewById(R.id.mine_info_head);
        head=(ImageView)findViewById(R.id.mine_info_head_img);


        final String names=sp.getString("name",null);
        String autographs=sp.getString("autograph",null);
        String brithdays=sp.getString("brithday",null);
        String sexs=sp.getString("sex",null);
        String img=sp.getString("img",null);

        name.setText(names);
        autograph.setText(autographs);
        brithday.setText(brithdays);
        sex.setText(sexs);

        try{
            byte[] byteArray= Base64.decode(img, Base64.DEFAULT);
            if(byteArray.length!=0){
                ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(byteArray);
                Bitmap bitmap= BitmapFactory.decodeStream(byteArrayInputStream);
                head.setImageBitmap(bitmap);
                this.bitmap=bitmap;
            }
        }catch (Exception e){
            e.printStackTrace();
        }


        fl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openAlbum();
            }
        });

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
                //设置广播
                try{
                    Intent intent=new Intent();
                    intent.setAction("MINE_INFO_CHANGES_ACTION");
                    MineInfo mineInfo=new MineInfo();
                    mineInfo.setName(name.getText().toString());
                    mineInfo.setAutograph(autograph.getText().toString());
                    bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
                    mineInfo.setBytes(outputStream.toByteArray());
                    intent.putExtra("mineinfo",mineInfo);
                    sendBroadcast(intent);
                }catch (Exception e){
                    e.printStackTrace();
                }


                //sp储存
                if ("".equals(name.getText().toString())){
                    Toast.makeText(MineInfoActivity.this,"请输入用户名",Toast.LENGTH_SHORT).show();
                }else{
                    try{
                        String imageString = new String(Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT));
                        sp.edit().putString("name",name.getText().toString()).commit();
                        sp.edit().putString("autograph",autograph.getText().toString()).commit();
                        sp.edit().putString("brithday",brithday.getText().toString()).commit();
                        sp.edit().putString("sex",sex.getText().toString()).commit();
                        sp.edit().putString("img",imageString).commit();
                        Toast.makeText(MineInfoActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
                    }catch (Exception e){
                        e.printStackTrace();
                    }


                }

            }
        });

    }


    /**
     * 打开系统相册
     */
    public void openAlbum(){
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        //设置请求码，以便我们区分返回的数据
        startActivityForResult(intent, 100);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==100){
            if (data != null) {
                try{
                    bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(data.getData()));
                    head.setImageBitmap(bitmap);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

}
